module.exports = function(grunt) {
    'use strict';

    require('time-grunt')(grunt);
    require('load-grunt-tasks')(grunt);
    grunt.loadNpmTasks('assemble');

    var reloadPort = 35729;

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        assemble: {
            options: {
                partials: ['assemble/src/include/**/*.hbs'],
                data: ['assemble/data/**/*.{json,yml}'],
                layout: 'assemble/src/template/layout/default.hbs'
            },
            pages: {
                cwd: 'assemble/src/template/doc',
                expand: true,
                src: ['*.hbs'],
                dest: '.tmp/'
            }
        },
        bower: {
            options: {},
            all: {
                rjsConfig: 'src/js/config.js'
            }
        },
        connect: {
            options: {
                port: 9000,
                hostname: 'localhost'
            },
            livereload: {
                options: {
                    middleware: (function(port) {
                        var lrSnippet = require('connect-livereload')({
                            port: port
                        }),
                        mountFolder = function(connect, dir) {
                            return connect.static(require('path').resolve(dir));
                        };

                        return function(connect) {
                            return [
                                lrSnippet,
                                mountFolder(connect, '.tmp')
                            ];
                        };

                    })(reloadPort)
                }
            }
        },
        clean: {
            dist: ['dist'],
            temp: ['.tmp']
        },
        copy: {
            live: {
                expand: true,
                dot: true,
                cwd: 'doc',
                dest: '.tmp/',
                src: ['.htaccess']
            }
        },
        jasmine: {
            spec: {
                src: 'src/js/{,*/}*.js',
                options: {
                    specs: 'spec/{,*/}*Spec.js',
                    template: require('grunt-template-jasmine-requirejs'),
                    templateOptions: {
                        requireConfigFile: 'src/js/config.js'
                    }
                }
            }
        },
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            gruntfile: ['Gruntfile.js'],
            src: ['src/js/{,*/}*.js'],
            spec: {
                options: {
                    jshintrc: 'spec/.jshintrc'
                },
                src: ['spec/{,*/}*.js']
            }
        },
        open: {
            server: {
                path: 'http://localhost:<%= connect.options.port %>'
            }
        },
        requirejs: {
            dist: {
                options: {
                    optimize: 'none',
                    include: ['main'],
                    preserveLicenseComments: false,
                    useStrict: true,
                    wrap: true,
                    mainConfigFile: 'src/js/config.js',
                    out: 'dist/<%= pkg.name %>.js'
                }
            }
        },
        watch: {
            assemble: {
                files: ['assemble/src/**/*.{hbs,json,yml}'],
                tasks: ['assemble']
            },
            livereload: {
                options: {
                    livereload: reloadPort
                },
                files: ['.tmp/{,*/}*.html']
            }
        }
    });
    
    grunt.registerTask('default', [
        'clean',
        'bower',
        'jshint',
        'jasmine'
    ]);

    grunt.registerTask('live', [
        'clean:temp',
        'assemble',
        'copy:live',
        'connect:livereload',
        'open',
        'watch'
    ]);
};